#pragma once
#include "comm_server.h"
#include "mutex.h"
#include <queue>
#include "buffer.h"
#include "win_event_lock.h"
#include <string>

namespace el
{
	class TcpServer;
	class TcpConnect;
	class TcpClient;
}

extern void RunDemo(void);
extern void RunEchoServer(unsigned short port);
extern void RunClient(std::string ip, unsigned short port);

class EchoServer
{
public:
	EchoServer(el::TcpServer *server);
	~EchoServer();

	void start();
	void stop();

private:
	void onMassage(el::TcpConnect *connect);

private:
	el::TcpServer *server_;
};

class EchoClient
{
public:
	EchoClient(el::TcpClient *client);
	~EchoClient();

	void send(const char* data, int len);
	bool waitSendFinish();

private:
	void connecting(int max_count);

	void onConnected(el::TcpConnect *connect);
	void onCloseConnected(el::TcpConnect *connect);
	void onMassage(el::TcpConnect *connect);

private:
	el::TcpClient *client_;
	el::TcpConnect *connect_;
	el::WinEventLock event_lock_;
	el::Mutex mutex_;
	std::queue<el::Buffer> queue_;
	el::Buffer current_buf_;
	bool is_ok_;
};